using GTA; // This is a reference that is needed! do not edit this
using GTA.Native; // This is a reference that is needed! do not edit this
using System; // This is a reference that is needed! do not edit this
using System.Windows.Forms; // This is a reference that is needed! do not edit this

namespace TruenoHeadlights
{
    public class TruenoHeadlights : Script // declare Modname as a script
    {
        VehicleDoor Headlights;
        int CloseDoorTimer;
        public TruenoHeadlights() // main function
        {
            Tick += OnTick;
            Interval = 10;
        }

        private void OnTick(object sender, EventArgs e) // This is where most of your script goes
        {

            Ped player = Game.Player.Character; // player variable
            Vehicle veh = player.CurrentVehicle; // vehicle variable

            /* Automatic Pop-up Headlights for various add-on vehicles 1.6.6
            This is the type of doors you can use:
            Headlights = GTA.VehicleDoor.BackLeftDoor;
            Headlights = GTA.VehicleDoor.BackRightDoor;
            Headlights = GTA.VehicleDoor.FrontLeftDoor;
            Headlights = GTA.VehicleDoor.FrontRightDoor;
            Headlights = GTA.VehicleDoor.Hood;
            Headlights = GTA.VehicleDoor.Trunk;
            Headlights = GTA.VehicleDoor.Trunk2;
            */

            if (Function.Call<bool>(Hash.IS_PED_IN_ANY_VEHICLE, player, false))
            {
                if (player.CurrentVehicle.Model == "ae86" || player.CurrentVehicle.Model == "trueno" || player.CurrentVehicle.Model == "rx7" || player.CurrentVehicle.Model == "tofu" || player.CurrentVehicle.Model == "rx7rb" || player.CurrentVehicle.Model == "TSUP3" || player.CurrentVehicle.Model == "miata3")
                {
                    Headlights = GTA.VehicleDoor.BackLeftDoor;
                    OpenOrClose();
                }

                if (player.CurrentVehicle.Model == "nsx2")
                {
                    Headlights = GTA.VehicleDoor.Hood;
                    OpenOrClose();
                }

				if (player.CurrentVehicle.Model == "mrtwo")
                {
                    Headlights = GTA.VehicleDoor.BackLeftDoor;
                    OpenOrClose();
                }
				
				if (player.CurrentVehicle.Model == "nsxtypr")
                {
                    Headlights = GTA.VehicleDoor.BackLeftDoor;
                    OpenOrClose();
                }
				
                if (CloseDoorTimer > 1 && CloseDoorTimer < 5)
                {
                    player.CurrentVehicle.CloseDoor(Headlights, false);
                }
            }
        }

        bool IsAnyLightOn()
        {
            return ((Game.Player.Character.CurrentVehicle.LightsOn == true) || (Game.Player.Character.CurrentVehicle.HighBeamsOn == true));
        }

        public void OpenOrClose()
        {
            if (IsAnyLightOn())
            {
                Game.Player.Character.CurrentVehicle.OpenDoor(Headlights, true, false);
                CloseDoorTimer = 0;
            }
            else
            {
                if (CloseDoorTimer < 5)
                CloseDoorTimer++;
            }
        }
    }
}